import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue' 
import Home from '../components/Home.vue'
import Desktop from  '../components/Desktop.vue'
import Plan from  '../components/Plan.vue'
import plan_2 from  '../components/plan_2.vue'
import Register from  '../components/Register.vue'
import User from  '../components/user/User.vue'
import UserInfo from  '../components/user/UserInfo.vue'
import UserList from  '../components/user/UserList.vue'
import UserDel from  '../components/user/UserDel.vue'
import UserModify from  '../components/user/UserModify.vue'
import Style from  '../components/Style.vue'
import File from  '../components/File.vue'
import P_recommend from  '../components/P_recommend.vue'
// import Recommendation from  '../components/Recommendation.vue'
// import Recommendation from  '../components/Recommendation.vue'
// import Recommendation from  '../components/Recommendation.vue'



Vue.use(VueRouter)

// const routes = [
  
// ]

const constantRouterMap = [
  { path:'/',redirect:'/login'},
  {path:'/login',component:Login },
  {path:'/Register',component:Register},
  {path:'/Desktop',component:Desktop},
  //
  // {
  //   path:'/user/User',
  //   component:User ,
  //   redirect:'/UserInfo',
  //   meta: {role:'user'},
  //   children:[{path:'/UserInfo',meta: {role:'user'},component:UserInfo},
  //             // {path:'/UserList',meta: {role:'admin'},component:UserList},
  //             {path:'/UserDel',meta: {role:'user'},component:UserDel},
  //             {path:'/UserModify',meta: {role:'user'},component:UserModify},
  //    ]
  // },

  // {
  //   path:'/Home',
  //   component:Home ,
  //   redirect:'/Plan',
  //   meta:{role:'user'},
  //   //home的子路由
  //   children:[{path:'/Plan',meta: {role:'user'},component:Plan},
  //             {path:'/plan_2',meta: {role:'user'},component:plan_2},
  //             {path:'/Style',meta: {role:'user'},component:Style},
  //             {path:'/P_recommend',meta: {role:'user'},component:P_recommend},
  //             {path:'/File',meta: {role:'user'},component:File},
  //   ]
  // }
]

const userRouterMap = [
  
  {
    path:'/user/User',
    component:User ,
    redirect:'/UserInfo',
    meta: {role:'user'},
    children:[{path:'/UserInfo',meta: {role:'user'},component:UserInfo},
              
     ]
  },

  {
    path:'/Home',
    component:Home ,
    redirect:'/Plan',
    meta:{role:'user'},
    //home的子路由
    children:[{path:'/Plan',meta: {role:'user'},component:Plan},
              {path:'/plan_2',meta: {role:'user'},component:plan_2},
              {path:'/Style',meta : {role:'user'},component:Style},
              {path:'/P_recommend',meta: {role:'user'},component:P_recommend},
              {path:'/File',meta: {role:'user'},component:File},
    ]
  }
  
]

const createRouter = () => new VueRouter({
    routes:constantRouterMap
  })

const router = createRouter()

function generateRoute(routes, role){
  let routeList = []
  routes.forEach((route)=>{
    const tmp = {...route}
    if(tmp.meta&&tmp.meta.role===role){
      if(tmp.children){
        tmp.children = generateRoute(tmp.children, role)
      }
      routeList.push(tmp)
    }
  })
  return routeList
}

//挂载路由导航守卫
router.beforeEach((to,from,next) => {
  //to 将要访问的路径
  //from 从哪个路径跳转而来
  //next 函数 表示放行

  const adminRouter = {path:'/UserList',meta: {role:'admin'},component:UserList}
  //访问登录页 直接放行
  let path = to.redirectedFrom || to.path;
  //获取token
  if(to.path ==='/login'||to.path ==='/Register') return next()
  // if (constantRouterMap.indexOf(path) >= 0) return next();
  const tokenStr = window.sessionStorage.getItem('token')
  // console.log( window.sessionStorage.getItem('token'))
  const role = window.sessionStorage.getItem('role')
  console.log(role)
  if(!tokenStr) {
     return next('./login')
  }
  console.log(router.getRoutes().length)
  if(router.getRoutes().length===4){
    if(role &&　role === 'user'){
      for(const element of userRouterMap )
        router.addRoute(element)
    }
    else if(role && role === 'admin'){
      router.addRoute(adminRouter)
    }
    console.log(router.getRoutes())
    // next(path)
    next({ ...to, replace: true })

  }
  console.log(from.path)
  console.log(path)
  next()

  //token 为空,说明没有登录,强制转到登录页
  // if(typeof(tokenstr) === undefined){
  //   const routes = generateRoute(asyncRouterMap,role)
  //   console.log(routes)
  //   for (const route of routes){
  //     router.addRoute(route)
  //   }
  //   next(next({
  //     ...to,
  //     replace: true
  //   }))
  // }
  // if(to.path ==='/login'||to.path ==='/Register') return next()
  // else{
  //   next()
  // }
  next()
  
})

export  function reset(){
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export  default router
